package com.ruoyi.flow.flow.form;

import com.ruoyi.flow.flow.workflow.FlowAssignee;
import com.ruoyi.flow.flow.workflow.FlowService;
import org.activiti.bpmn.model.*;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.repository.ProcessDefinition;

import java.io.Serializable;

/**
 * 流程节点关联的表单信息类
 * Created by fenghuo on 2018/8/28.
 */
public class FormInfo implements Serializable {
    /**
     * 表单标识
     */
    private String formKey;
    /**
     * 判断下一节点是否是网关。
     * <br/>该属性用于供客户端决定是否需要根据网关动态获取下一节点是否需要选择办理人。可能不同的网关条件对应的目标节点需要选择不同的办理人
     */
    private boolean nextNodeIsGateway;
    /**
     * 判断下一个节点是否需要选择办理人。如果是，则需要客户端提供选择办理人的界面。
     * <br/>
     * 如果{@code nextNodeIsGateway==true}，则可以忽略该属性，因为需要根据网关动态获取下一个节点是否需要选择不同的办理人。
     */
    private String nextSelectAssignee;

    public FormInfo(Task task) {
        if (task instanceof UserTask) {
            UserTask userTask = (UserTask) task;
            this.formKey = userTask.getFormKey();
            FlowNode flowNode = FlowService.activityService().getNextNode(userTask);
            if (flowNode instanceof ExclusiveGateway) {
                this.nextNodeIsGateway = true;
            } else if (flowNode instanceof Task) {
                FlowAssignee nextTaskAssignee = FlowService.taskService().getNextTaskSelectAssignee((Task) flowNode);
                if (nextTaskAssignee != null && nextTaskAssignee.getValueType() == FlowAssignee.ValueType.Select) {
                    this.nextSelectAssignee = nextTaskAssignee.getValue();
                }
            }
        }
    }

    public FormInfo(ProcessDefinition definition) {
        String definitionId = definition.getId();
        StartEvent startEvent = FlowService.activityService().getStartEvent(definitionId);
        String formKey = startEvent.getFormKey();
        this.formKey = formKey;
        setFormInfo(definitionId);
    }

    public FormInfo(TaskEntity task) {
        String taskId = task.getId();
        String formKey = task.getFormKey();
        this.formKey = formKey;
        setFormInfo(taskId);
    }

    private void setFormInfo(String id) {
        // 获取下一步办理人.如果下一步是网关，则这里不获取，而是根据用户选择的网关条件动态获取。
        FlowNode nextFlowNode = FlowService.activityService().getNextNode(id);
        if (nextFlowNode != null && nextFlowNode instanceof ExclusiveGateway) {
            this.setNextNodeIsGateway(true);
        } else {
            FlowAssignee nextSelectAssignee = FlowService.taskService().getNextTaskSelectAssignee(id, null);
            if (nextSelectAssignee != null && nextSelectAssignee.getValueType() == FlowAssignee.ValueType.Select) {
                this.setNextSelectAssignee(nextSelectAssignee.getValue());
            }
        }
    }

    public String getFormKey() {
        return formKey;
    }

    public void setFormKey(String formKey) {
        this.formKey = formKey;
    }

    public boolean isNextNodeIsGateway() {
        return nextNodeIsGateway;
    }

    public void setNextNodeIsGateway(boolean nextNodeIsGateway) {
        this.nextNodeIsGateway = nextNodeIsGateway;
    }

    public String getNextSelectAssignee() {
        return nextSelectAssignee;
    }

    public void setNextSelectAssignee(String nextSelectAssignee) {
        this.nextSelectAssignee = nextSelectAssignee;
    }
}
